package com.taixu.apikey.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.taixu.common.annotation.RepeatSubmit;
import com.taixu.common.annotation.Log;
import com.taixu.common.core.controller.BaseController;
import com.taixu.common.core.domain.PageQuery;
import com.taixu.common.core.domain.R;
import com.taixu.common.core.validate.AddGroup;
import com.taixu.common.core.validate.EditGroup;
import com.taixu.common.enums.BusinessType;
import com.taixu.common.utils.poi.ExcelUtil;
import com.taixu.apikey.domain.vo.ApiKeyVo;
import com.taixu.apikey.domain.bo.ApiKeyBo;
import com.taixu.apikey.service.IApiKeyService;
import com.taixu.common.core.page.TableDataInfo;

/**
 * 平台秘钥
 *
 * @author ximu
 * @date 2023-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/service/apikey")
public class ApiKeyController extends BaseController {

    private final IApiKeyService iApiKeyService;

    /**
     * 查询平台秘钥列表
     */
    @SaCheckPermission("service:apikey:list")
    @GetMapping("/list")
    public TableDataInfo<ApiKeyVo> list(ApiKeyBo bo, PageQuery pageQuery) {
        return iApiKeyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出平台秘钥列表
     */
    @SaCheckPermission("service:apikey:export")
    @Log(title = "平台秘钥", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ApiKeyBo bo, HttpServletResponse response) {
        List<ApiKeyVo> list = iApiKeyService.queryList(bo);
        ExcelUtil.exportExcel(list, "平台秘钥", ApiKeyVo.class, response);
    }

    /**
     * 获取平台秘钥详细信息
     *
     * @param keyid 主键
     */
    @SaCheckPermission("service:apikey:query")
    @GetMapping("/{keyid}")
    public R<ApiKeyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long keyid) {
        return R.ok(iApiKeyService.queryById(keyid));
    }

    /**
     * 新增平台秘钥
     */
    @SaCheckPermission("service:apikey:add")
    @Log(title = "平台秘钥", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ApiKeyBo bo) {
        return toAjax(iApiKeyService.insertByBo(bo));
    }

    /**
     * 修改平台秘钥
     */
    @SaCheckPermission("service:apikey:edit")
    @Log(title = "平台秘钥", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ApiKeyBo bo) {
        return toAjax(iApiKeyService.updateByBo(bo));
    }

    /**
     * 删除平台秘钥
     *
     * @param keyids 主键串
     */
    @SaCheckPermission("service:apikey:remove")
    @Log(title = "平台秘钥", businessType = BusinessType.DELETE)
    @DeleteMapping("/{keyids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] keyids) {
        return toAjax(iApiKeyService.deleteWithValidByIds(Arrays.asList(keyids), true));
    }
}
